
package com.scs.application.modules.base.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.model.*;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.HospitalCatalog;
import com.scs.application.modules.base.entity.MaterialCatalog;
import com.scs.application.modules.base.request.HospitalCatalogQueryRequest;
import com.scs.application.modules.base.service.HospitalCatalogService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 医院品类 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/base/hospital_catalogs")
public class HospitalCatalogController extends CrudController<HospitalCatalog, String, HospitalCatalogService> implements ImAndExAbleController<HospitalCatalogQueryRequest> {


    @GetMapping("/page")
    public PageResult page(HospitalCatalogQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(HospitalCatalogQueryRequest request) {
        List<HospitalCatalog> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    @GetMapping("/tree")
    public CommonResult tree(HospitalCatalogQueryRequest request) {
        List<HospitalCatalog> list = service.list(request);
        TreeNode root = null;
        if (request.isAutoAddRoot()) {
            root = new TreeNode();
            root.setId("0");
            root.setTitle("医院品类");
        }
        TreeModel<HospitalCatalog> treeModel = new TreeModel<HospitalCatalog>(list, "name", root);
        return CommonResult.ok("查询成功", treeModel.asList());
    }

    @Override
    public CommonResult add(@RequestBody @Valid HospitalCatalog entity) {
        entity.setHospitalId(UserUtils.getHospitalId());
        return super.add(entity);
    }

    /***
     * @Description:  通用删除，多个
     */
    @DeleteMapping("/deleteBatchCommon")
    public RestResult deleteBatchCommon(@RequestBody Map<String, Object> para) {
        BusinessException.throwErrorIf(!UserUtils.isHospital(),"当前非医院角色，不允许操作");

        List<Serializable> ids = (List<Serializable>) para.get("ids");
        //是否有下属商品
        int count = service.count(Wrappers.<HospitalCatalog>query().in("parent_id", ids));
        if (count > 0) {
            throw new BusinessException("品类存在下级，不允许删除");
        }
        Boolean isOk = service.removeByIds(ids);
        if (!isOk) {
            return RestResult.error("数据删除失败<br>可能所操作数据已被删除，请刷新页面重新尝试操作");
        }
        return RestResult.ok("数据删除成功");
    }

    /***
     * @param id
     * @Description:  通用查找，单个
     */
    @GetMapping("/findByIdCommon")
    public RestResult findByIdCommon(@RequestParam("id") Serializable id) {
        HospitalCatalog entity = service.getById(id);
        if (entity == null) {
            return RestResult.error("数据查询失败<br>可能所操作数据已被删除，请刷新后重新尝试操作");
        }
        if (StringUtils.isNotBlank(entity.getParentId()) && !"0".equals(entity.getParentId())) {
            HospitalCatalog entityParent = service.getById(entity.getParentId());
            if (entityParent != null) {
                entity.setParentName(entityParent.getName()).setParentId(entityParent.getId());
            }
        } else{
            entity.setParentName("商品分类").setParentId("0");
        }

        return RestResult.ok("获取数据成功", entity);
    }

    /***
     * @param entity
     * @Description:  通用保存、更新
     */
    @PostMapping("/saveOrUpdateCommon")
    public RestResult saveOrUpdateCommon(@Valid @RequestBody HospitalCatalog entity) {
        BusinessException.throwErrorIf(!UserUtils.isHospital(),"当前非医院角色，不允许操作");

        entity.setHospitalId(UserUtils.getHospitalId());
        boolean isOk = service.saveOrUpdate(entity);
        if (!isOk) {
            return RestResult.error("数据保存失败");
        }
        return RestResult.ok("数据保存成功", entity);
    }
}
